# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=openscad
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2021.01
pkgrel=17
pkgdesc="The programmers solid 3D CAD modeller (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
license=("spdx:GPL-2.0-or-later")
url="https://www.openscad.org/"
msys2_repository_url="https://github.com/openscad/openscad"
msys2_references=(
  'cpe: cpe:/a:openscad:openscad'
)
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-boost-libs"
         "${MINGW_PACKAGE_PREFIX}-cairo"
         "${MINGW_PACKAGE_PREFIX}-double-conversion"
         "${MINGW_PACKAGE_PREFIX}-fontconfig"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-gettext-runtime"
         "${MINGW_PACKAGE_PREFIX}-glew"
         "${MINGW_PACKAGE_PREFIX}-glib2"
         "${MINGW_PACKAGE_PREFIX}-gmp"
         "${MINGW_PACKAGE_PREFIX}-harfbuzz"
         "${MINGW_PACKAGE_PREFIX}-libwinpthread-git"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-libzip"
         "${MINGW_PACKAGE_PREFIX}-mpfr"
         "${MINGW_PACKAGE_PREFIX}-opencsg"
         "${MINGW_PACKAGE_PREFIX}-qscintilla-qt5"
         "${MINGW_PACKAGE_PREFIX}-qt5-base"
         "${MINGW_PACKAGE_PREFIX}-qt5-svg"
         "${MINGW_PACKAGE_PREFIX}-qt5-multimedia"
         "${MINGW_PACKAGE_PREFIX}-shared-mime-info")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             #"${MINGW_PACKAGE_PREFIX}-cmake"
             #"${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-boost"
             "${MINGW_PACKAGE_PREFIX}-cgal"
             "${MINGW_PACKAGE_PREFIX}-eigen3"
             "${MINGW_PACKAGE_PREFIX}-imagemagick")
source=("https://files.openscad.org/${_realname}-${pkgver}.src.tar.gz"
        '001-openscad-2021-libintl-collisions.patch'
        '002-openscad-2021-remove-print-polyhedron.patch'
        '003-fix-linking-hidapi-clang.patch'
        '004-fix-building-against-cgal5.4.patch::https://github.com/openscad/openscad/commit/abfebc65.patch'
        '005-fix-building-against-newer-boost.patch::https://github.com/openscad/openscad/commit/08bf69b4.patch'
        '006-fix-building-against-boost-1.85.patch'
        '007-fix-linking-to-opengl.patch')
sha256sums=('d938c297e7e5f65dbab1461cac472fc60dfeaa4999ea2c19b31a4184f2d70359'
            'f295d41896cc55f6645e080e527ba3d033357bc744b7f7bb01b9271974ee930e'
            '43cebd358d152f7a6dc1fc27294c67c4938b207d6a06f26fd60117e3a3c71412'
            '0606fe5b76f1f95c23c9dfde8d4e66adf36285c141a5cc11da107022eee297fe'
            '836626d08bd8dcce496bd2db0a73ae1911915388c412dc63d831d40ae1aa9206'
            '583783a6ecb2fe7d23b37bc5e29c6c705065748a6993908ff28ad61b10190ce7'
            'c649ce4a6b7b840764955c9c3dee5f7d68b72c2665c8bb71b962554844f43ad4'
            '536471fd3ceaa69fe338a45b7300b0b058f7af4b3b99bf87be27423d91f74e9b')

apply_patch_with_msg() {
  for _fname in "$@"
  do
    msg2 "Applying ${_fname}"
    patch -Nbp1 -i "${srcdir}"/${_fname}
  done
}

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}
  # https://github.com/openscad/openscad/commit/4c5d19c3
  apply_patch_with_msg \
    001-openscad-2021-libintl-collisions.patch
  # https://github.com/openscad/openscad/commit/9aa0d7e9
  apply_patch_with_msg \
    002-openscad-2021-remove-print-polyhedron.patch
  apply_patch_with_msg \
    003-fix-linking-hidapi-clang.patch \
    004-fix-building-against-cgal5.4.patch \
    005-fix-building-against-newer-boost.patch \
    006-fix-building-against-boost-1.85.patch \
    007-fix-linking-to-opengl.patch

  # CGAL 5.4 compatibility
  sed -i "s|CGAL_VERSION_NR < 1050401000|CGAL_VERSION_NR < CGAL_VERSION_NUMBER(5, 4, 0)|" src/cgalutils-tess.cc
  #rm -f libraries/MCAD/*.py
  convert "${srcdir}"/${_realname}-${pkgver}/icons/${_realname}.png -resize 128x128\> "${srcdir}"/${_realname}-${pkgver}/icons/${_realname}-128.png
}

build() {
  msg2 "Will be in future"
  # mkdir -p build-${MSYSTEM} && cd build-${MSYSTEM}

  # declare -a _extra_config
  # if check_option "debug" "n"; then
  #   _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  # else
  #   _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  # fi

  # MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  # ${MINGW_PREFIX}/bin/cmake.exe \
    # -G"Ninja" \
    # -DCMAKE_PREFIX_PATH=${MINGW_PREFIX} \
    # "${_extra_config[@]}" \
    # ../${_realname}-${pkgver}

  # ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  cd "${srcdir}"/${_realname}-${pkgver}

  local -a _extra_config
  if check_option "debug" "y"; then
    _extra_config+=(CONFIG+=debug)
  else
    _extra_config+=(CONFIG+=release)
  fi

  BOOSTDIR=${MINGW_PREFIX} \
  CGALDIR=${MINGW_PREFIX} \
  EIGENDIR=${MINGW_PREFIX} \
  GLEWDIR=${MINGW_PREFIX} \
  OPENCSGDIR=${MINGW_PREFIX} \
  ${MINGW_PREFIX}/bin/qmake \
    "${_extra_config[@]}" \
    PREFIX="${pkgdir}${MINGW_PREFIX}" \
    VERSION="${pkgver}" \
    BOOST_LINK_FLAGS="-lboost_thread-mt -lboost_program_options-mt -lboost_filesystem-mt"

  make
  make INSTALL_ROOT= install
  install -Dm644 "icons/${_realname}.xml" "${pkgdir}${MINGW_PREFIX}/share/mime/packages/${_realname}.xml"

  # cd build-${MSYSTEM}
  # DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .

  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/COPYING "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/COPYING
}
